//
//  RGInviteRecordsVC.m
//  RedGrass
//
//  Created by wuyine on 2019/2/14.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGInviteRecordsVC.h"
#import "RGInviteRecordsCell.h"
#import "UIScrollView+EmptyDataSet.h"
#import <MJRefresh.h>
#import "HttpRequest.h"

@interface RGInviteRecordsVC ()<DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) MJRefreshHeader *mjHeader;
@property (nonatomic,strong) MJRefreshAutoNormalFooter *mjFooter;
@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,strong) NSMutableArray *dataList;

@end

@implementation RGInviteRecordsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNavBar.title = @"邀请记录";
    [self showBackButton];
    self.customContentView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
    [self.customContentView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [_mjHeader beginRefreshing];
    [self loadFirstPage];
}

- (void)loadFirstPage {
    self.pageNum = 1;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/user/invitationRecord"];
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    NSDictionary *params = @{@"token":SafeStr(user.token),
                            @"pageNumber":@(self.pageNum),
                            @"pageSize":@(PageSize)
                            };
  
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.mjHeader endRefreshing];
        weakSelf.tableView.emptyDataSetDelegate = self;
        weakSelf.tableView.emptyDataSetSource = self;
        weakSelf.dataList = [RGinvitationRecordListModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        [weakSelf.tableView reloadData];
        
        if (weakSelf.dataList.count < PageSize) {
            weakSelf.mjFooter.state = MJRefreshStateNoMoreData;
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.mjHeader endRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}

- (void)loadNextPage {
    self.pageNum += 1;
    HttpRequest *quest = [HttpRequest buildRequest:url_rechargeRecord];
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    NSDictionary *params = @{@"token":SafeStr(user.token),
                             @"pageNumber":@(self.pageNum),
                             @"pageSize":@(PageSize)
                             };
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.mjFooter endRefreshing];
        NSArray *list = [RGinvitationRecordListModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        if (list) {
            [weakSelf.dataList addObjectsFromArray:list];
            [weakSelf.tableView reloadData];
        }
        if (list.count < PageSize) {
            [weakSelf.mjFooter endRefreshingWithNoMoreData];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.mjFooter endRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 74;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RGInviteRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"invite"];
    if(self.dataList.count){
        RGinvitationRecordListModel *model = _dataList[indexPath.row];
        cell.model = model;
    }
    return cell;
}

- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return UICOLOR_WITH_RGBINT(0x000000);
}

- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    UIFont *font = [UIFont systemFontOfSize:17];
    NSDictionary *attribute = @{NSFontAttributeName:font,
                                NSForegroundColorAttributeName:UICOLOR_WITH_RGBINT(0xffffff),
                                };
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"你还没有邀请记录哦" attributes:attribute];
    return title;
}

- (nullable NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    UIFont *font = [UIFont systemFontOfSize:14];
    NSDictionary *attribute = @{NSFontAttributeName:font,
                                NSForegroundColorAttributeName:UICOLOR_WITH_RGBINT(0xdddddd),
                                };
    NSAttributedString *description = [[NSAttributedString alloc] initWithString:@"" attributes:attribute];
    return description;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[RGInviteRecordsCell class] forCellReuseIdentifier:@"invite"];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        _tableView.mj_header = self.mjHeader;
        _tableView.mj_footer = self.mjFooter;
    }
    return _tableView;
}

- (MJRefreshHeader *)mjHeader {
    if (!_mjHeader) {
        _mjHeader = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadFirstPage)];
    }
    return _mjHeader;
}

-(MJRefreshAutoNormalFooter *)mjFooter{
    if(!_mjFooter){
        _mjFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNextPage)];
        [_mjFooter setTitle:@"" forState:MJRefreshStateNoMoreData];
    }
    return _mjFooter;
}

@end
